-- do_gamble
-- Created by chengb Jul/17/2015
-- 成就：赌博

-- 内部函数声明
local whenDoGamble;

-- 注册赌博的回调
EventMgr.register("achievements/do_gamble", event.DO_GAMBLE, function(args)
    whenDoGamble(args);
end);

-- 注册赌博的回调
whenDoGamble = function(args)
    local result = args.result;
    result = iif(result == true, 1, 0);

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("do_gamble");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end
        
        -- 判断赌博结果是否匹配
        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["result"]) == 'number' and 
               ruleArg["result"] ~= result then
                return false;
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};